<?php class HeatherList extends HeatherObject {
	protected $_Type = '';
	protected function NewList ($Operator = '*') {
		$this->AddChild (new HeatherList ($Operator));
	}
	protected function NewItem () {
		$this->AddChild (new HeatherItem ());
	}
	public function __construct ($Operator = '*') {
		switch ($Operator) {
			case '*': $this->_Type = 'Bulleted'; break;
			case '-': $this->_Type = 'Numbered'; break;
			default: trigger_error ("unknown list operator [$Operator], list creation aborted", E_USER_ERROR);
		}
	}


	protected function ParseIndent ($String) {
		$Result = array ('', '', $String);
		$Matches = array ();
		if (preg_match ('[^ (\s*) ([-*]) \s (.*) $]ux', $String, $Matches)) $Result = array ($Matches [1], $Matches [2], $Matches [3]);
		return ($Result);
	}



	public function AddString ($String) {
		list ($WSPrefix, $Operator, $Text) = $this->ParseIndent ($String);
		if ($Operator) {
			if ($WSPrefix) {
				// strip one level and recurse
				if ("\t" !== $WSPrefix [0]) {
					$WSPrefix = (strlen ($WSPrefix) > 1) ? substr ($WSPrefix, 2) : '';
				} else $WSPrefix = substr ($WSPrefix, 1);
				$this->LastChildClassCheck (__CLASS__) or $this->NewList ($Operator);
				$Text = $WSPrefix.$Operator.' '.$Text;
			} else {
				// item
				$this->NewItem ();
			}
		} else {
			// plain string
			$this->NoLastChild () and $this->NewItem ();
		}
		call_user_func (array ($this->LastChild, __FUNCTION__), $Text);
	}



/*
	public function PushLine (HeatherLine $Line, $Level) {
		if ($Level) {
			if ($this->NoLastChild () or ! ($this->LastChild instanceof HeatherList)) {
				if ($Level > 1) trigger_error ('Looks like some hierarchy levels are missing. Please, fix that.', E_USER_ERROR);
				$this->NewList ($Line);
			}
			$this->LastChild->PushLine ($Line, $Level - 1);
		} else {
			if ($this->NoLastChild () or ! ($this->LastChild instanceof HeatherItem) or '*' === $Line->Operator or '-' === $Line->Operator) $this->NewItem ();
			$this->LastChild->PushLine ($Line);
		}
	}
*/
	protected function XMLAttributes () {
		$Result = '';
		strlen ($this->_Type) and $Result .= ' type="'.htmlspecialchars ($this->_Type, ENT_QUOTES).'"';
		return ($Result);
	}
} ?>